<template>
  <div>
    <el-card style="margin-top:20px;">
      <h3>品类管理</h3>
      <el-button @click="toCategoryAdd">添加品类</el-button>
      <p>当前商品分类ID：0</p>
      <el-table :data="levaleOneList" stripe style="width: 100%">
        <el-table-column prop="id" label="品类ID"></el-table-column>
        <el-table-column prop="name" label="品类名称"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="editName(scope.row)">修改名称</el-button>
            <el-button size="mini" @click="checkChildren(scope.row)">查看其子品类</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="收货地址" :visible.sync="dialogEditVisible">
      <el-form :model="editInfo">
        <el-form-item label="请输入新的品类名称">
        <el-input v-model="editInfo.id"></el-input>
      </el-form-item>
        <el-form-item label="请输入新的品类名称">
        <el-input v-model="editInfo.name"></el-input>
      </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { productType } from "../util/http";
import { categoryEdit } from "../util/http";
export default {
  // 组件名称
  name: "demo", // 组件参数 接收来自父组件的数据
  props: [],
  components: {},
  data() {
    return {
      levaleOneList: [],
      dialogEditVisible: false,
      editInfo: ""
    };
  },
  computed: {},
  watch: {},
  methods: {
    async getLevalOne() {
      const { data: res } = await productType(
        `/api/manage/category/get_category.do?categoryId=0`
      );
      this.levaleOneList = res.data;
    },
    editName(info) {
      this.dialogEditVisible = true;
      this.editInfo = info;
    },
    async editSure() {
      const {data:res} = await categoryEdit(`/api/manage/category/set_category_name.do?categoryId=${this.editInfo.id}&categoryName=${this.editInfo.name}`)
      // console.log(res)
      if( res.status !== 0 ) this.$message.error("更新品类名字失败");
      this.getLevalOne();
      this.dialogEditVisible = false;
      this.$message.success("更新品类名字成功");
    },
    checkChildren(info) {
      this.$router.push('/secondCategory')
      this.$store.commit('setSecondCategory',info)
    },
    toCategoryAdd(){
      this.$router.push('/categoryAdd')
    }
  },
  beforeCreate() {},
  created() {},
  beforeMount() {},
  mounted() {
    this.getLevalOne();
  }
};
</script> 
<style scoped>
</style>
